package com.example.send.controller;

import com.example.send.service.HttpAPIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class BlogController {

    @Autowired
    private HttpAPIService httpAPIService;

    @RequestMapping("/MayflyCHG/ajax/{sufPath}")
    public String ajax1(@PathVariable String sufPath, HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("sufPath：" + sufPath);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/ajax/{suf1Path}/{suf2Path}")
    public String ajax2(@PathVariable String suf1Path, @PathVariable String suf2Path, HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("sufPath：" + (suf1Path + "/" + suf2Path));
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/tag/")
    public String tag1(HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("tag：tag/");
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/p/{sufPath}")
    public String p(@PathVariable String sufPath, HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("p sufPath：" + sufPath);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/tag/{sufPath}")
    public String tag2(@PathVariable String sufPath, HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("tag sufPath：" + sufPath);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/archive/{sufPath}")
    public String archive(@PathVariable String sufPath, HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("archive sufPath：" + sufPath);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/category/{sufPath}")
    public String category(@PathVariable String sufPath, HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("category sufPath：" + sufPath);
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    @RequestMapping("/MayflyCHG/default.html")
    public String defaultPage(HttpServletRequest request) throws Exception {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println("defalut：/default.html");
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxx");
        return getString(request);
    }

    public String getString(HttpServletRequest request) throws Exception {
        return getString(request, null);
    }

    public String getString(HttpServletRequest request, String fromUrl) throws Exception {
        String url = request.getRequestURI();
        String queryString = request.getQueryString();
        if (queryString != null && queryString.length() > 0) {
            queryString = "?" + queryString;
        } else {
            queryString = "";
        }
        if (fromUrl == null || fromUrl.length() == 0) {
            fromUrl = "https://www.cnblogs.com";
        }
        String forward = fromUrl + url + queryString;
        System.out.println("forward:" + forward);
        return httpAPIService.doGet(forward);
    }

}
